$(document).ready(function() {
	// Change search
	var searchChange = $("header #workPlace .searchOption .search .select a");
	var googleSearch = $("header #workPlace .searchOption .search .select a.searchByGoogle");
	var izwebzSearch = $("header #workPlace .searchOption .search .select a.searchByIzwebz");
	var optionSelect = $('header #workPlace .searchOption .search select option');
	var optionSelectGoogle = $('header #workPlace .searchOption .search select option[value="google"]');
	var optionSelectIzwebz = $('header #workPlace .searchOption .search select option[value="izwebz"]');
	googleSearch.click(function(event) {
		searchChange.removeClass('none','active');
		$(this).addClass('none');
		izwebzSearch.addClass('active');
		optionSelect.removeAttr("selected");
		optionSelectIzwebz.attr("selected","selected");
	});
	izwebzSearch.click(function(event) {
		searchChange.removeClass('none','active');
		$(this).addClass('none');
		googleSearch.addClass('active');
		optionSelect.removeAttr("selected");
		optionSelectGoogle.attr("selected","selected");
	});


	var searchText = $('header #workPlace .searchOption .search input[name="searchText"]');
	var searchOption = $('header #workPlace .searchOption');
	var searchSuggest = $('header #workPlace .searchOption .searchSuggest');
	searchText.click(function(event) {
		searchSuggest.show();
	});
	searchText.focusin(function(event) {
		searchSuggest.show();
	});
	searchOption.mouseleave(function(event) {
		/* Act on the event */
		searchSuggest.hide()
	});


	// FLY MENU
	// 
	var fly = $("#singlePost .postDetail .flyMenu");

	$(window).scroll(function () {
		if ($(this).scrollTop() > 190) {
			fly.addClass('fixMenu')
		}else{
			fly.removeClass('fixMenu')
		};	
	})
	
	var authorPanel = $(".singleQuiz");

	authorPanel.waypoint(function(dir) {
		if (dir == "down") {
			fly.animate({opacity: 0},50);	
		}else{
			fly.animate({opacity: 1},50);	
		};
	  	
	},{ 
		offset: 610
	});

	var sponsor = $("#izSpon");
	sponsor.waypoint(function(dir){
		if (dir == "down") {
			sponsor.addClass('fixMenu')
		}else{
			sponsor.removeClass('fixMenu')
		};
	})

	$('.radioChange input:radio').screwDefaultButtons({ 
		image: "url(images/radio.png)",
		width:	 20,
		height:	 20
	});

	var backTop = $("#backTop");
	$(window).scroll(function(event) {
		/* Act on the event */
		if ($(this).scrollTop() > 300) {
			backTop.show(300);
		}else{
			backTop.hide(300);
		};
	});

	//added new
	var article = $("article#newPost .postArticle");
	var labelItem = $("ul.label li");
	article.css('display', 'none');
	$("article#newPost .postArticle:first").css('display', 'block');
	
	labelItem.click(function(event) {
		/* Act on the event */
		var data = $(this).find('a').attr("href").substr(1);
		console.log(data);
		article.css('display', 'none');
		$("#"+data).css('display', 'block');
		return false;
	});
	
	var rating = $(".rating");
	var progress = $(".rating progress");
	var proInt = progress.attr("value");

	rating.each(function(index, el) {
		var ints = $(this).find('progress').attr("value");
		if (ints >= 75) {
			$(this).find('progress').addClass('pblue');
			var textReturn = "Hàng tốt";
		}else{
			var textReturn = "Hàng lỏm";
		};
		$(this).find('.reviewStatus').html(textReturn);
	});
	
	// 
	var rateWidget = $("#ratingWiget .rating");
	rateWidget.each(function(index, el) {
		var currentValue = $(this).find('input[type="range"]').attr("value");
		$(this).find('output').html(currentValue+"/100")
		$(this).find('input[type="range"]').change(function(event) {
			$(this).parent().find("output").html($(this).val()+"/100")

		});
	});

	$(".sendRating").click(function(event) {
		/* Act on the event */
		rateWidget.each(function(index, el) {
			var currentValue = $(this).find('input[type="range"]').val();
			if (currentValue >= 75) {
				var addNewClass = "pblue";
			}else{
				var addNewClass = "";
			};
			$(this).find('output').remove();
			$(this).find('input[type="range"]').remove();
			$(this).html('<progress class="'+addNewClass+'" value="'+currentValue+'" max="100"></progress>');
		});
		$(".sendRating").remove();
		return false;
	});




	var	toc = $(".toc");
	var tableContentsUl = $(".tableContents ul");
	for (var i = 0; i < toc.length; i++) {
		var id = toc[i].id;
		var content = toc[i].innerHTML;
		tableContentsUl.append('<li><a href="#'+id+'">'+content+'</a></li>');
	};






















});
